//
//  CollecionViewController.m
//  豆瓣
//
//  Created by hy on 16/1/7.
//  Copyright © 2016年 hongyu. All rights reserved.
//

#import "CollecionViewController.h"
#import "HyCollectionViewCell.h"
#import "HyModel.h"
#import "UIImageView+WebCache.h"
@interface CollecionViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>
@property (nonatomic, retain) NSMutableArray *modelArray;
@end

@implementation CollecionViewController

- (void)dealloc {
    [_modelArray release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"电影";
    self.navigationController.navigationBar.translucent = NO;
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    self.navigationController.navigationBar.barStyle = UIStatusBarStyleLightContent;
    [self creatcollection];
    [self fhitembutton];
    [self data];
}
/**数据解析*/
- (void)data {
    NSString *path = [[NSBundle mainBundle] pathForResource:@"movie" ofType:@"plist"];
    NSArray *arr = [NSArray arrayWithContentsOfFile:path];
    NSLog(@"%@", arr);
    self.modelArray = [NSMutableArray array];
    for (NSDictionary *dic in arr) {
        HyModel *model = [HyModel model];
        [model setValuesForKeysWithDictionary:dic];
        [_modelArray addObject:model];
    }
}

- (void)creatcollection {
//  创建瀑布流样式
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
//    滚动的方向
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
//    item大小
    layout.itemSize = CGSizeMake((375 - 40) / 3, 150);
//    行高列高
    layout.minimumLineSpacing = 10;
    layout.minimumInteritemSpacing = 10;
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
//    创建collectionview
     UICollectionView *collection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 59) collectionViewLayout:layout];
    collection.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:collection];
    [collection release];
    [layout release];
    [collection registerClass:[HyCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    collection.delegate = self;
    collection.dataSource = self;
}
- (void)fhitembutton {
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"zs.png"] style:UIBarButtonItemStylePlain target:self action:@selector(itemAction:)];
    self.navigationItem.rightBarButtonItem = item;
    [item release];
}
//itembutton方法
- (void)itemAction:(UIBarButtonItem *)item {
    [self dismissViewControllerAnimated:YES completion:^{
    }];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _modelArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    HyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    HyModel *model = _modelArray[indexPath.row];
    [cell.hyImageView sd_setImageWithURL:[NSURL URLWithString:model.image] placeholderImage:[UIImage imageNamed:@"111.jpg"]];
    cell.hyLabel.text = model.title;
    return cell;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
